import * as types from '../constants/incomeTypes'

const initialState = {
  isSuccess: false,
  // errMsg: '',
  stat: {
    totalIncome: '0.00', // 总收入
    todayIncome: '0.00', // 今日收入
    // totalFreeze: '0.00', // 冻结金额
    expandSubFreeze: '0.00',
    withdrawable: '0.00', // 可提现
    totalWithdrawal: '0.00', // 已经提现
    balance: '0.00' // 账户余额
  }
}

export default function income (state = initialState, action) {
  switch (action.type) {
    case types.INCOME_IN_SUCCESS:
      return {
        stat: handleFixed2(action.income),
        isSuccess: 'success'
      }
      break
    case types.INCOME_IN_ERROR:
      return {
        ...state,
        errMsg: action.err,
        isSuccess: 'err'
      }
      break

    default:
      return state
  }
}

function handleFixed2 (income) {
  const stat = initialState.stat
  for (let i in stat) {
    if (stat.hasOwnProperty(i)) {
      income[i] = Number(income[i]).toFixed(2) || '0.00'
    }
  }
  return income
}
